package com.aiphoto.upload.controller;

import com.aiphoto.upload.service.AiMonitoringService;
import com.aiphoto.upload.util.AliyunOSSUtil;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;

public class UploadController {
    @Autowired
    private AiMonitoringService aiMonitoringService;

    public Map upload() {
        Map map = new HashMap();
        BufferedReader br = null;
        File file1 = new File("C:\\FIS\\v6.500\\path_next.txt");
        if (!file1.exists()) {
            System.out.println("文本不存在!");
            return map;
        }
        try {
            br = new BufferedReader(new InputStreamReader(new FileInputStream("C:\\FIS\\v6.500\\path_next.txt"), "GB2312"));//文本编码读取,防止中文乱码
            if (br == null) {
                return map;
            }
            String contentLine;

            while ((contentLine = br.readLine()) != null) {
                //            contentLine = br.readLine();
                String s = contentLine.substring(0, 6);
                //读取每一行，并输出
                //System.out.println(s.length());
                //System.out.println(s);
                if (s.equals("[value")) {
                    //System.out.println("错误!");
                } else {
                    //System.out.println(">>>>>>>>>>>>>>>>>>>>>>");
                    String str1 = contentLine.substring(0, contentLine.indexOf("="));
                    //System.out.println("str1:"+str1);
                    String str2 = contentLine.substring(str1.length() + 1, contentLine.length());
                    map.put(str1, str2);
                    //System.out.println(map);
                    if (str1.equals("path")) {
//                        String url = new String(str2.getBytes("UTF-8"),"UTF-8");
//                        System.out.println("url:" + url);
                        File file = new File(str2);
                        if (!file1.exists()) {
                            System.out.println("地址无效!");
                        }else {
                            String Host = "AIPhoto";
                            String uploadUrl = AliyunOSSUtil.upload(file, Host);
                            map.put(str1, uploadUrl);
                        }
                    }
                }
            }
            return map;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (br != null) {
                    br.close();
                }
            } catch (IOException e) {
                System.out.println("Error in closing the BufferedReader");
            }
        }
        return map;
    }
}
